package com.koron.ticket.line.workFake;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import com.koron.common.core.util.StringUtils;
import com.koron.ticket.fake.work.bean.SecurityFakeBean;
import com.koron.ticket.fake.work.bean.WorkInfoFakeBean;
import com.koron.ticket.fake.work.mapper.SecurityFakeMapper;
import com.koron.ticket.fake.work.mapper.WorkInfoFakeMapper;
import com.koron.ticket.line.workFake.bean.WorkFakeBean;
import com.koron.ticket.line.workFake.bean.dto.WorkFakeDto;
import com.koron.ticket.line.workFake.mapper.WorkFakeMapper;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: rocky
 * @Date: 2022/1/5 14:38
 */
@Service("lineWorkFakeService")
public class WorkFakeService {

    private EamUser getCurrUser() {
        return ThreadLocalContext.get();
    }


    @TaskAnnotation("fakeSave")
    public String fakeSave(SessionFactory factory, WorkFakeDto dto){

        WorkFakeMapper mapper = factory.getMapper(WorkFakeMapper.class);//样票主表
        WorkInfoFakeMapper workinfomapper = factory.getMapper(WorkInfoFakeMapper.class);//样票详情
        SecurityFakeMapper securitymapper = factory.getMapper(SecurityFakeMapper.class);//样票安全措施

        WorkFakeBean bean = Convert.convert(WorkFakeBean.class, dto);
        if(StringUtils.isBlank(dto.getId())){
            String id  = CodeTools.getCode32();
            bean.setId(id);
            mapper.insert(bean);

            //样票详情入库
            if(ObjectUtil.isNotNull(dto.getWorkInfo())){
                WorkInfoFakeBean workinfofake = dto.getWorkInfo();
                workinfofake.setId(CodeTools.getCode32());
                workinfofake.setFakeTicketId(id);
                workinfomapper.insertSelective(workinfofake);
            }

            //样票安全措施入库
            if(StringUtils.isNotEmpty(dto.getSecurityArrangementList())){
                List<SecurityFakeBean> list = dto.getSecurityArrangementList();
                list.stream().forEach(se->{
                    se.setId(CodeTools.getCode32());
                    se.setFakeTicketId(id);
                });
                securitymapper.batchInsert(list);
            }

        }else{
            mapper.updateByPrimaryKeySelective(bean);

            //样票详情入库
            workinfomapper.deleteByFakeTicketId(dto.getId());
            if(ObjectUtil.isNotNull(dto.getWorkInfo())){
                WorkInfoFakeBean workinfofake = dto.getWorkInfo();
                workinfofake.setFakeTicketId(dto.getId());
                workinfomapper.insertSelective(workinfofake);
            }

            //样票安全措施入库
            securitymapper.deleteByFakeTicketId(dto.getId());
            if(StringUtils.isNotEmpty(dto.getSecurityArrangementList())){
                List<SecurityFakeBean> list = dto.getSecurityArrangementList();
                list.stream().forEach(se->{
                    se.setId(CodeTools.getCode32());
                    se.setFakeTicketId(dto.getId());
                });
                securitymapper.batchInsert(list);
            }
        }

        return bean.getId();
    }


    @TaskAnnotation("details")
    public WorkFakeDto details(SessionFactory factory, String id){

        WorkFakeMapper mapper = factory.getMapper(WorkFakeMapper.class);//样票主表
        WorkInfoFakeMapper workinfomapper = factory.getMapper(WorkInfoFakeMapper.class);//样票详情
        SecurityFakeMapper securitymapper = factory.getMapper(SecurityFakeMapper.class);//样票安全措施

        WorkFakeBean workFakeBean = mapper.selectByPrimaryKey(id);
        WorkFakeDto vo = Convert.convert(WorkFakeDto.class, workFakeBean);

        WorkInfoFakeBean workinfo = workinfomapper.findFirstByFakeTicketId(id);//样票详情
        vo.setWorkInfo(workinfo);

        List<SecurityFakeBean> securityfake = securitymapper.queryByFakeTicketId(id);//样票安全措施
        vo.setSecurityArrangementList(securityfake);

        return vo;
    }


}
